    // pages/job-offering/job-offering.js
    Page({

        /**
         * 页面的初始数据
         */
        data: {
            no_more: false, //是否有更多数据？默认为false
            size: 5,
            step: 0,
            popupShow: true, //弹出层是否[可以]显示
            inputValue: '',
            dataList_area: {
                '': []
            },
            //需请求云函数动态传入！
            //格式如下:
            // dataList_area: {
            //     '广州市': ['不限', '越秀区', '海珠区', '荔湾区', '天河区', '白云区', '黄埔区', '花都区', '番禺区', '南沙区', '从化区', '增城区'],
            //     '重庆市': [],
            //     '长春市': [],
            //     '成都市': [],
            //     '长沙市': [],
            //     '大连市': [],
            //     '不限': []
            // },
            dataList_position: {
                '不限': [],
                '幼儿园': ['不限', '专任老师', '其他老师'],
                '小学': ['不限', '语文老师', '数学老师', '英语老师', '音乐老师', '体育老师', '美术老师', '科学老师', '心理健康老师', '信息技术老师', '其他老师'],
                '初中': ['不限', '语文老师', '数学老师', '英语老师', '物理老师', '化学老师', '生物老师', '历史老师', '政治老师', '地理老师', '音乐老师', '体育老师', '美术老师', '道德与法治', '心理健康老师', '信息技术老师', '通用技术老师', '其他老师'],
                '高中': ['不限', '语文老师', '数学老师', '英语老师', '物理老师', '化学老师', '生物老师', '历史老师', '政治老师', '地理老师', '音乐老师', '体育老师', '美术老师', '道德与法治', '心理健康老师', '信息技术老师', '通用技术老师', '其他老师'],
                // '高职': ['不限', '语文老师', '数学老师', '英语老师', '思想品德老师', '科学技术老师'],
                '中学': ['不限', '语文老师', '数学老师', '英语老师', '物理老师', '化学老师', '生物老师', '历史老师', '政治老师', '地理老师', '音乐老师', '体育老师', '美术老师', '道德与法治', '心理健康老师', '信息技术老师', '通用技术老师', '其他老师'],
                '中小学': ['不限', '语文老师', '数学老师', '英语老师', '物理老师', '化学老师', '生物老师', '历史老师', '政治老师', '地理老师', '音乐老师', '体育老师', '美术老师', '道德与法治', '心理健康老师', '信息技术老师', '通用技术老师', '其他老师'],
                '职校': ['职校老师'],
                '特殊教育': ['特殊教育老师'],
                '其他': ['其他老师']

            },
            filterArray_apply: [{
                    title: '报名方式',
                    buttonList: ['不限', '在线报名', '现场报名']
                },
                {
                    title: '报名时间',
                    buttonList: ['不限', '正在报名', '即将报名', '结束报名']
                }
            ],
            filterArray_more: [{
                    title: '编制',
                    buttonList: ['不限', '编制内', '编制外']
                },
                {
                    title: '发布时间',
                    buttonList: ['不限', '今天', '最近7天', '最近1个月', '最近3个月', '最近1年', ]
                }

            ],
            //用于请求云函数的筛选参数,记录当前页面已经选择了哪个筛选项
            filterOptions: {
                city: '', //市
                district: '', //区
                post_level: '', //岗位等级
                subject: '', //科目
                registration_way: '', //报名方式
                has_organized: '', //是否有编制
                stage: '', //报名阶段
                choosetime: 0 //发布时间    
            },
            curPosition: {
                province: "广东省",
                city: "广州市"
            }
            // teacherList: [{},{}...]请求返回的结果
        },

        //请求teacherList
        get_teacherList_onLoad() {
            const that = this
            console.log("position:", this.data.curPosition)
            wx.$cloud.callFunction({
                name: "getPost",
                data: {
                    Action: 'getPosts',
                    province: that.data.curPosition.province,
                    city: that.data.curPosition.city,
                    skip: 0,
                    size: that.data.size
                },
            }).then(
                res => {
                    this.setData({
                        teacherList: res.result.data,
                        step: this.data.step + this.data.size
                    })
                    console.log(this.data.teacherList)
                    //判断是否还有更多结果
                    if (res.error === 0) {
                        this.setData({
                            no_more: true
                        })
                    }
                }
            )
        },

        //请求dataList_area
        async get_dataList_area_onLoad() {
            //函数:取城市信息
            const getCityArr = async () => {
                const res = await wx.$cloud.callFunction({
                    name: 'getCityInf',
                    data: {
                        action: "getCities",
                        limit: 100
                    },
                })
                return res.result.data
            }
            //取得城市信息
            const cityArr = await getCityArr()
            //函数:由城市信息取出区的信息，进而返回所筛选所需要的数据结构
            const getDataListArea = async (cityArr) => {
                //将城市信息转化为筛选组件需要的数据格式,存放在dataList_area中
                const dataList_area = {}
                //在市级筛选项添加'不限'项
                dataList_area["不限"] = []
                for (const item of cityArr) {
                    if (item.city) { //若该市的名称存在
                        //获取该市的具体信息
                        const cityDetail = await wx.$cloud.callFunction({
                            name: 'getCityInf',
                            data: {
                                action: "getAreas",
                                cityName: item.city
                            }
                        })
                        const key = item.city + "市" //市名:String
                        const value = cityDetail.result.data[0].area //该市包含的区名:Array
                        // console.log('key', key)
                        // console.log('value', value)
                        dataList_area[key] = value
                    }
                }
                return dataList_area
            }
            //取得筛选所需要的数据结构
            /*
            const dataList_area = await getDataListArea(cityArr)
            */
            var temp = await wx.$cloud.callFunction({
                name: 'getCityInf',
                data: {
                    action: "getAllInfo",
                }
            })
            temp = temp.result
            var dataList_area = {}
            dataList_area["不限"] = []
            for (let i = 0; i < temp.length; i++) {
                dataList_area[temp[i].city + '市'] = temp[i].area
            }
            console.log(dataList_area)
            //设置dataList_area
            this.setData({
                dataList_area: dataList_area,
            })
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad(options) {
            const that = this
            // 请求定位信息
            {
                wx.getLocation({
                    success(r) {
                        wx.request({
                            url: 'https://apis.map.qq.com/ws/geocoder/v1',
                            data: {
                                location: '' + r.latitude + ',' + r.longitude,
                                key: '3F3BZ-2MT6X-SBG4O-7ZUFM-E6PAO-OXB3Q'
                            },
                            success(res) {
                                if (res.data.status != 0) {
                                    wx.showToast({
                                        title: '定位失败',
                                        icon: "error"
                                    })
                                    const pos = {
                                        province: '广东省',
                                        city: '广州市',
                                    }
                                    that.setData({
                                        curPosition: pos
                                    })
                                    var temp_pos = wx.getStorageSync('curPosition')
                                    if (temp_pos != undefined && temp_pos.province != undefined) {
                                        that.setData({
                                            "curPosition.province": temp_pos.province,
                                            "curPosition.city": temp_pos.city
                                        })
                                    }
                                    console.log('pppos', that.data.curPosition);
                                    that.get_teacherList_onLoad() //请求teacherList
                                } else {
                                    const pos = {
                                        province: res.data.result.address_component.province,
                                        city: res.data.result.address_component.city,
                                    }
                                    that.setData({
                                        curPosition: pos
                                    })
                                    var temp_pos = wx.getStorageSync('curPosition')
                                    if (temp_pos != undefined && temp_pos.province != undefined) {
                                        that.setData({
                                            "curPosition.province": temp_pos.province,
                                            "curPosition.city": temp_pos.city
                                        })
                                    }
                                    console.log('pppos', that.data.curPosition);
                                    that.get_teacherList_onLoad() //请求teacherList
                                }

                            }
                        })
                    },
                    fail() {
                        const pos = {
                            province: '广东省',
                            city: '广州市',
                        }
                        that.setData({
                            curPosition: pos
                        })
                        var temp_pos = wx.getStorageSync('curPosition')
                        if (temp_pos != undefined && temp_pos.province != undefined) {
                            that.setData({
                                "curPosition.province": temp_pos.province,
                                "curPosition.city": temp_pos.city
                            })
                        }
                        console.log('pppos', that.data.curPosition);
                        that.get_teacherList_onLoad() //请求teacherList
                    }
                })
            }
            this.get_dataList_area_onLoad() //请求dataList_area
            //判断缓存中是否存在look_limit字段
            wx.getStorage({

                key: 'look_limit',

                success: function (res) {

                    // 异步接口在success回调才能拿到返回值
                    console.log("成功获取到数据")
                    console.log("数据：", res.data)
                    that.setData({
                        look_limit: res.data
                    })
                },

                fail: function () {

                    console.log('未创建该变量')
                    wx.setStorage({
                        key: "look_limit",
                        data: 0,
                        success: function () {

                            console.log('写入value1成功')

                        },

                        fail: function () {

                            console.log('写入value1发生错误')

                        }

                    })

                }
            })
        },
        onShow: function () {
            const that = this
            var temp_pos = wx.getStorageSync('curPosition')
            var province = that.data.curPosition.province
            var city = that.data.curPosition.city
            if (province != undefined && city != undefined) {
                if (province != temp_pos.province || city != temp_pos.city) {
                    if (temp_pos.province != undefined && temp_pos.city != undefined) {
                        that.setData({
                            "curPosition.province": temp_pos.province,
                            "curPosition.city": temp_pos.city
                        })
                        that.get_teacherList_onLoad()
                    }
                }
            }
        },

        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function () {
            console.log('我到底啦!')
            if (this.data.no_more === true) {
                console.log('没有更多数据!')
                return
            }
            // 判断当前是否有筛选条件
            var filted = false
            const filterOptionsKeys = Object.keys(this.data.filterOptions)
            filterOptionsKeys.forEach(k => {
                if (k != 'choosetime')
                    if (this.data.filterOptions[k])
                        filted = true
            })
            // 未筛选时使用getPost接口
            var cloudOptions = {
                name: 'getPost',
                data: {
                    Action: 'getPosts',
                    province: this.data.curPosition.province,
                    city: this.data.curPosition.city,
                    skip: this.data.step,
                    size: this.data.size,
                },
            }
            // 有筛选时使用return-recruit-info接口
            if (filted)
                cloudOptions = {
                    name: 'return-recruit-info',
                    data: {
                        action: 'return',
                        size: this.data.size,
                        step: this.data.step,
                        ...this.data.filterOptions
                    }
                }
            wx.$cloud.callFunction({
                ...cloudOptions
            }).then(
                res => {
                    console.log('onReachBottom@@@@@@@@@@@@@@@', res)
                    if (res.result.error != 0) { //若返回不为空
                        this.setData({
                            teacherList: [...this.data.teacherList, ...res.result.data],
                            step: this.data.step + this.data.size
                        })
                    } else { //否则，没有更多结果了,设置no_more为真
                        this.setData({
                            no_more: true
                        })
                    }
                    console.log(this.data.teacherList)

                }
            )
        },

        commonConfirmFilter() { //通用的筛选确定操作
            //重置step和no_more
            this.setData({
                step: 0,
                no_more: false
            })
            //相当于刷新一下，起到了收回筛选栏的效果
            this.setData({
                popupShow: false
            })
            this.setData({
                popupShow: true
            })
            //请求
            wx.$cloud.callFunction({
                name: 'return-recruit-info',
                data: {
                    action: 'return',
                    size: this.data.size,
                    step: this.data.step,
                    ...this.data.filterOptions
                }
            }).then(
                res => {
                    console.log('筛选确定后返回结果:', res)
                    this.setData({
                        teacherList: res.result.data,
                        step: this.data.step + this.data.size
                    })
                }
            )

        },

        //第1个筛选确定之后触发
        confirmFilter1(e) {
            console.log('筛选1确定了')

            const filterOptions = {
                ...this.data.filterOptions
            }
            if (e.detail.firstVal != '不限') {
                filterOptions.city = e.detail.firstVal
            } else {
                filterOptions.city = ''
            }
            if (e.detail.secondVal != '不限') {
                filterOptions.district = e.detail.secondVal
            } else {
                filterOptions.district = ''
            }
            this.setData({
                filterOptions: filterOptions
            })
            console.log(this.data.filterOptions)

            this.commonConfirmFilter()
        },

        //第2个筛选确定之后触发
        confirmFilter2(e) {
            console.log('筛选确定了2')

            const filterOptions = {
                ...this.data.filterOptions
            }
            if (e.detail.firstVal != '不限') {
                filterOptions.post_level = e.detail.firstVal
            } else {
                filterOptions.post_level = ''
            }
            if (e.detail.secondVal != '不限') {
                filterOptions.subject = e.detail.secondVal
            } else {
                filterOptions.subject = ''
            }
            this.setData({
                filterOptions: filterOptions
            })
            console.log(this.data.filterOptions)

            this.commonConfirmFilter()
        },

        //第3个筛选确定之后触发
        confirmFilter3(e) {
            console.log('筛选确定了3')

            const filterOptions = {
                ...this.data.filterOptions
            }
            if (e.detail.selectedValue[0] != '不限') {
                filterOptions.registration_way = e.detail.selectedValue[0]
            } else {
                filterOptions.registration_way = ''
            }
            if (e.detail.selectedValue[1] != '不限') {
                filterOptions.stage = e.detail.selectedValue[1]
            } else {
                filterOptions.stage = ''
            }
            this.setData({
                filterOptions: filterOptions
            })
            console.log(this.data.filterOptions)

            this.commonConfirmFilter()
        },

        //第4个筛选确定之后触发
        confirmFilter4(e) {
            console.log('筛选确定了4')

            const filterOptions = {
                ...this.data.filterOptions
            }
            if (e.detail.selectedValue[0] != '不限') {
                filterOptions.has_organized = e.detail.selectedValue[0]
            } else {
                filterOptions.has_organized = ''
            }

            if (e.detail.selectedValue[1] == '不限') {
                filterOptions.choosetime = 0
            } else if (e.detail.selectedValue[1] == '今天') {
                filterOptions.choosetime = 1
            } else if (e.detail.selectedValue[1] == '最近7天') {
                filterOptions.choosetime = 7
            } else if (e.detail.selectedValue[1] == '最近1个月') {
                filterOptions.choosetime = 30
            } else if (e.detail.selectedValue[1] == '最近3个月') {
                filterOptions.choosetime = 90
            } else if (e.detail.selectedValue[1] == '最近1年') {
                filterOptions.choosetime = 360
            }
            this.setData({
                filterOptions: filterOptions,
            })
            console.log(this.data.filterOptions)

            this.commonConfirmFilter()
        },

        async JumpToSearch() { //搜索函数
            const that = this
            //先判断是否登录
            if (getApp().globalData.isLogin == undefined) {
                wx.$cloud.callFunction({
                    name: "inviteUser",
                    data: {
                        sign: 5
                    },
                    success: res => {
                        //如果该用户未登陆过
                        if (res.result.length == 0) {
                            wx.getStorage({
                                key: 'look_limit',
                                success: function (res) {
                                    // 异步接口在success回调才能拿到返回值
                                    console.log("成功获取到数据")
                                    console.log("数据：", res.data)
                                    that.setData({
                                        look_limit: res.data
                                    })
                                    if (that.data.look_limit >= 3) {
                                        wx.showToast({
                                            title: '请先登录',
                                            icon: "error",
                                            duration: 1000
                                        })
                                        setTimeout(function () {
                                            wx.navigateTo({
                                                url: "/pages/login_main/login_main",
                                                success: (result) => {

                                                },
                                                fail: () => {},
                                                complete: () => {}
                                            });
                                        }, 1000)
                                    } else {
                                        that.setData({
                                            look_limit: that.data.look_limit + 1
                                        })
                                        wx.setStorage({
                                            key: "look_limit",
                                            data: that.data.look_limit,
                                        })

                                        that.search_temp()
                                    }
                                },

                                fail: function () {
                                    console.log('未创建该变量')
                                    wx.setStorage({
                                        key: "look_limit",
                                        data: 0,
                                    })

                                }
                            })
                            /*
                    wx.showToast({
                      title: '请先登录',
                      icon: "error",
                      duration: 1000
                    })
                    setTimeout(function(){
                        wx.navigateTo({
                            url: "/pages/login_main/login_main",
                            success: (result) => {
                  
                            },
                            fail: () => {},
                            complete: () => {}
                        });
                    },1000)*/
                        } else {
                            getApp().globalData.isLogin = true
                            this.conclVip()
                        }
                    }
                })
            } else {
                this.conclVip()
            }

        },

        async conclVip() {
            // let temp=await wx.cloud.callFunction({
            //     name: "vipLimit",
            //     data: {
            //         sign: 0,
            //     }
            // })
            // temp=temp.result
            // if(temp.errCode==1){
            //     wx.showToast({
            //       title: '查看次数已用完!',
            //       icon: 'error',
            //       duration: 2000,
            //       success:res =>{
            //         setTimeout(function(){
            //             wx.navigateTo({
            //                 url: '../../pages/huiyuan/huiyuan',
            //               })
            //         },2000)
            //       }
            //     })

            //     return ;
            // }
            if (this.data.inputValue == "") return;
            this.setData({
                searchValue: this.data.inputValue
            })
            //props是给search页面传递的参数
            const props = {
                flag: null,
                teacherList: null,
                searchValue: this.data.searchValue,
                size: this.data.size,
                step: this.data.size
            }

            await wx.$cloud.callFunction({
                name: "return-recruit-info",
                data: {
                    action: 'search',
                    search: this.data.inputValue,
                    size: this.data.size,
                    step: 0
                }
            }).then(
                res => {
                    if (res.result.data.length == 0) { //若没找到或出错
                        props.flag = 0
                    } else {
                        props.flag = 2
                        props.teacherList = [...res.result.data]
                    }
                    console.log('serach@@@@@@@@@@@@@@@@@@@@@@', res)

                }
            )

            wx.navigateTo({
                url: '/pages/search/search',
                events: {
                    //仅用于传递参数
                    propsFunc() {}
                },
                success: (res) => {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('propsFunc', props)
                },
                fail: () => {},
                complete: () => {}
            });
        },

        async search_temp() {
            if (this.data.inputValue == "") return;
            this.setData({
                searchValue: this.data.inputValue
            })
            //props是给search页面传递的参数
            const props = {
                flag: null,
                teacherList: null,
                searchValue: this.data.searchValue,
                size: this.data.size,
                step: this.data.size
            }

            await wx.$cloud.callFunction({
                name: "return-recruit-info",
                data: {
                    action: 'search',
                    search: this.data.inputValue,
                    size: this.data.size,
                    step: 0
                }
            }).then(
                res => {
                    if (res.result.data.length == 0) { //若没找到或出错
                        props.flag = 0
                    } else {
                        props.flag = 2
                        props.teacherList = [...res.result.data]
                    }
                    console.log('serach@@@@@@@@@@@@@@@@@@@@@@', res)

                }
            )

            wx.navigateTo({
                url: '/pages/search/search',
                events: {
                    //仅用于传递参数
                    propsFunc() {}
                },
                success: (res) => {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('propsFunc', props)
                },
                fail: () => {},
                complete: () => {}
            });
        },

        bindKeyInput(e) {
            this.setData({
                inputValue: e.detail.value
            })
        },
    })